/**
 * Created by Freeman on 2016/7/14.
 */

import {USER_SELECT,SEND_TEXT,WRITE_TEXT} from '../constants/ActionTypes'
import UUID from 'uuid-js'
let now = new Date();
const initialState = {
    items: [
        {
            userId: 2,
            messages: [
                {
                    text: 'Hello，这是一个基于React + Redux + Webpack构建的简单chat示例，聊天记录保存在Redux。简单演示了React和Redux的基础特性和webpack配置。',
                    date: now,
                    _id : UUID.create().toString(),
                    self: true
                },
                {
                    text: '项目地址: https://git.oschina.net/fury-me/react-redux-chat',
                    date: now,
                    _id : UUID.create().toString(),
                    self: true
                },
                {
                    text: 'React is good!',
                    date: now,
                    _id : UUID.create().toString(),
                }
            ]
        },
        {
            userId: 3,
            messages: []
        }
    ],
    sessionIndex:0
}

export default function sessionlist(state=initialState,action) {
    switch (action.type){
        case USER_SELECT:
            return {
                ...state,sessionIndex:action.sessionIndex
            };
        case SEND_TEXT:{
            let items = state.items;

            items[state.sessionIndex].messages.push({
                text: action.text,
                date: new Date(),
                _id : UUID.create().toString(),
                self: true
            });
            return {
                ...state,
                items:[
                    ...state.items,
                    items
                ],
            };
        }
        default :
            return state;
    }
}